package hhq.demo.dailybilling.controller;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import hhq.demo.dailybilling.dao.DailyBillingDao;
import hhq.demo.dailybilling.model.DailyBill;

@RestController
@RequestMapping("/dailybill")
public class DailyBillingController {

    @Autowired
    DailyBillingDao dao;
 
    // http://localhost:9966/huohuaqi/dailybill/insert
    @RequestMapping(value="/insert", method = RequestMethod.POST)
    public String insertDailyBill(
        @RequestParam("UID") String UID, 
        @RequestParam("entry_time") String entry_time, 
        @RequestParam("money") String money,
        @RequestParam("intro") String intro
    ){
        System.out.println(UID);
        System.out.println(entry_time);
        System.out.println(money);
        System.out.println(intro);
        
        try{
            LocalDate localDate = LocalDate.parse(entry_time);
            BigDecimal moneyDec = new BigDecimal(money); 
            DailyBill b=new DailyBill();
            b.setUID(Long.parseLong(UID));
            b.setEntry_time(localDate);
            b.setMoney(moneyDec);
            b.setIntro(intro);
            dao.insertOne(b);
        }catch(Exception e){
            System.err.println("insertDailyBill 错误："+e);
            e.printStackTrace();
            return "N";
        } 
 
        return "Y";
    }

    // http://localhost:9966/huohuaqi/dailybill/queryOneUserAll
    @RequestMapping(value="/queryOneUserAll", method = RequestMethod.GET)
    public String queryOneUserAll(
        @RequestParam("UID") String UID,
        @RequestParam("begin_date") String begin_date,
        @RequestParam("end_date") String end_date
    ){
        long uid = Long.parseLong(UID);
        List<DailyBill> list =  dao.queryOneUserAll(uid, begin_date, end_date);
        JSONObject json=new JSONObject(); // res, arr
        json.put("res", "Y");
        JSONArray arr=new JSONArray();

        for(DailyBill b: list){
            JSONObject item = (JSONObject)JSONObject.toJSON(b);
            //System.out.println("money:" + item.toJSONString());
            arr.add(item);
        }

        json.put("arr", arr);

        return json.toJSONString();
    }

    // http://localhost:9966/huohuaqi/dailybill/delete_bill
    @RequestMapping(value="/delete_bill", method = RequestMethod.GET)
    public String delete_bill(
        @RequestParam("billID") long billID
    ){ 
        
        try {
            dao.deleteOne(billID);
            return "Y";
        } catch (Exception e) {
           e.printStackTrace();
        }

        return "N";
    }
    
}
